package PenguinsFarm.Penguins
{	
	import PenguinsFarm.Penguins.Stage.IPenguinsStage;
	import PenguinsFarm.Penguins.Stage.IdleStage;
	import PenguinsFarm.Penguins.Stage.MovingStage;
	import PenguinsFarm.PenguinsFarmView;
	import PenguinsFarm.Renderer.CageBackground;
	
	import RendererUltility.FilterEffect;
	
	import XMFramework.Interface.Architechture.SMView;
	import XMFramework.Interface.IFramework;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	public class PenguinsRenderer extends Sprite
	{
		public static var ON_MOUSE_EVENT:String =	"ON_MOUSE_EVENT";
		
		private var stageAnimPtr:IPenguinsStage	=	null;
		private var mcPenguin:MovieClip			=	null;
		private var mcSwimmingContainer:MovieClip;
		private var isLiving:Boolean			=	false;	
		
		private var isShowingInfo:Boolean		=	false;
		
		private var owner:PenguinsEntity			=	null;
		
		public function PenguinsRenderer( owner:PenguinsEntity )
		{
			super();
			
			this.owner = owner;
		}
		
		public function startRendering():void
		{
			addEventListener( MouseEvent.CLICK, handleMouseEvent );
			addEventListener( MouseEvent.ROLL_OUT, handleMouseEvent );
			addEventListener( MouseEvent.ROLL_OVER, handleMouseEvent );
			
			var farmView:PenguinsFarmView = Global.CoreSNSFramework.getArchView( ShareMacros.MODULE_PENGUINS_FARM ) as PenguinsFarmView;
			if( ! farmView.getCageBackground().isBackgroundLoaded() )
				farmView.getCageBackground().addEventListener( CageBackground.ON_BACKGROUND_LOADED, onBGLoaded, false, 0, true );
				
			else
				giveLife();
		}
		
		private function onBGLoaded( e:Event ):void
		{
			(Global.CoreSNSFramework.getArchView( ShareMacros.MODULE_PENGUINS_FARM ) as PenguinsFarmView).getCageBackground()
				.removeEventListener( CageBackground.ON_BACKGROUND_LOADED, onBGLoaded, false );
		}
		
		private function handleMouseEvent( e:MouseEvent ):void
		{	
			dispatchEvent( new Event( PenguinsRenderer.ON_MOUSE_EVENT ) );
			
			switch( e.type )
			{				
				case MouseEvent.ROLL_OUT:
					filters	=	null;
					if(isShowingInfo)
					{
						//Global.CoreSNSFramework.hideView((ShareMacros.MV_TooltipPenguinsInfo);
						isShowingInfo		=	false;
					}
					break;
				
				case MouseEvent.ROLL_OVER:
					filters		=	FilterEffect.createStrokeFilter( 0xffffff, 3.0, 0, 1 );

					if(!isShowingInfo)
					{
						// Lấy dữ liệu từ PeculiarData cho chim tương ứng
						//var data:Dictionary = PeculiarData.singleton.getResourceDict(owner.logicalObj.TypeId);
						//Global.CoreSNSFramework.showView(ShareMacros.MV_TooltipPenguinsInfo, false, data[owner.logicalObj.PenguinsId], true);
						
						/*var tooltipLayer:TooltipPenguinsInfo = Global.viewsCab.getLayer(ShareMacros.MV_TooltipPenguinsInfo) as TooltipPenguinsInfo;
						tooltipLayer.x	=	(e.stageX + tooltipLayer.width) < SFramework.singleton.getWidth() ? e.stageX : e.stageX - tooltipLayer.width;
						tooltipLayer.y	=	(e.stageY + tooltipLayer.height) < SFramework.singleton.getHeight() ? e.stageY : e.stageY - tooltipLayer.height;*/
						
						isShowingInfo		=	true;
					}
					
					break;
			}
		}
		
		private function giveLife():void
		{
			isLiving		=	true;
			var from:Point	=	(Global.CoreSNSFramework.getArchView( ShareMacros.MODULE_PENGUINS_FARM 
											) as PenguinsFarmView).getCageBackground().getRandomGroundPoint();
			this.x			=	from.x;
			this.y			=	from.y;
		}
		
		public function applySwimmingStage( apply:Boolean ):void
		{
			if( mcSwimmingContainer == null )
			{
				var cls:Class		=	Global.CoreSNSFramework.resourceManager.getImmediateResourceFunc( 
											ShareConstant.SWF_CommonAsset, "SwimmingPenguins" ) as Class;
				mcSwimmingContainer	=	new cls;				
			}
			
			if( apply )
			{
				if( mcPenguin.parent !== mcSwimmingContainer.container )
				{
					removeChild( mcPenguin );
					mcSwimmingContainer.container.addChild( mcPenguin );
					addChild( mcSwimmingContainer );
				}
			}
			else
			{
				addChild( mcPenguin );
				if( mcSwimmingContainer.parent )
					removeChild( mcSwimmingContainer );
			}
		}
		
		public function onInit( configData:Dictionary, initData:Object ):void
		{
			// Add default penguin
			Global.CoreSNSFramework.resourceManager.getResourceFunc( ShareConstant.SWF_CommonAsset, "LoadingPenguins", onLoadingDefaultAsset );
			
			// Start download real penguin asset
			var type:String	=	initData.type;
			var arr:Object	=	Global.CoreSNSFramework.configData.getMetadata( "penguins" );
			
			var isFind:Boolean = false;
			for( var i : int = 0; !isFind && i < arr.length; ++i )
			{
				if( arr[i].hasOwnProperty( "ID" ) && arr[i]["ID"] == type )
				{
					Global.CoreSNSFramework.resourceManager.getResourceFunc( arr[i]["Bucket"], arr[i]["Symbol"], onLoadingAsset );
					isFind = true;
				}
			}
		}
		
		private function onLoadingDefaultAsset( bucket:String, className:String, cls:Class ):void
		{
			mcPenguin		=	new cls;
			addChild( mcPenguin );
			stageAnimPtr	=	new IdleStage( this );
		}
		
		private function onLoadingAsset( bucket:String, className:String, cls:Class ):void
		{
			// Remove default penguins
			if( mcPenguin.parent )
				mcPenguin.parent.removeChild( mcPenguin );
			
			mcPenguin		=	new cls;
			addChild( mcPenguin );			
		}
		
		public function onFrameUpdate( deltaTime : Number, totalTime : Number ):void
		{	
			if( isLiving == false )
				return;
			
			if( getAssetPtr() == null )
				return;
			
			if( isShowingInfo == true )
				return;
			
			if( stageAnimPtr )
			{
				if( stageAnimPtr.isCompleteStage() )
					changeRandomStage();
				else
					stageAnimPtr.onFrameUpdate( deltaTime );
			}
			else
				changeRandomStage();
		}
		
		public function getAssetPtr():MovieClip
		{
			return mcPenguin;
		}
		
		protected function changeRandomStage():void
		{
			if( stageAnimPtr )
				stageAnimPtr.dispose();
			
			if( (Math.floor(Math.random()*2) + 1) >= 2 ? true : false)
			{
				stageAnimPtr	=	new MovingStage( this );				
			}
			else
				stageAnimPtr	=	new IdleStage( this );
		}
		
		public function set PenguinsStage( stage:IPenguinsStage ):void
		{
			stageAnimPtr		=	stage;
		}		
	}
}